package net.zjitc.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import net.zjitc.domain.User;
import net.zjitc.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/2/27.
 */
@Controller
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User>{
    private Log log= LogFactory.getLog(this.getClass());
    private User model = new User();
    @Resource
    private UserService userService;



    public String loginUI(){
        return "loginUI";
    }
    public String login(){
        //如果有用户的话，返回登陆成功
        User user = userService.findByNamePassword(model.getUsername(),model.getPassword());

        if(user!=null){
            log.debug("登陆成功！");
            log.error("登陆成功！............");
          this.addActionMessage("登陆成功！");
/*            将对象放入值栈
            ActionContext.getContext().getValueStack().push(user);*/
            ActionContext.getContext().getSession().put("user",user);
            List<User> userList = userService.findAll();
            /*将取到的集合放到值栈空间*/
            ActionContext.getContext().put("userList",userList);
            return "loginSuccess";
        }
        this.addFieldError("username","用户名不存在");
        this.addActionError("用户名或密码错误");
        return "loginFail";
    }

    @Override
    public User getModel() {
        return model;
    }
}
